package com.luozy.item.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luozy.common.dto.PageDTO;
import com.luozy.item.mapper.ItemMapper;
import com.luozy.item.pojo.Item;
import com.luozy.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //1.分页查询商品
    @Override
    public PageDTO<Item> listPage(Integer page, Integer size) {
        //分页参数
        IPage<Item> iPage = new Page<>(page, size);
        itemMapper.selectPage(iPage, null);
        //获取分页结果
        long total = iPage.getTotal();
        List<Item> records = iPage.getRecords();
        //封装并返回
        return new PageDTO<>(total, records);
    }

    //3.新增商品
    @Override
    public void addItem(Item item) {
        if (item.getName() == null || item.getPrice() == null || item.getStock() == null) {
            throw new RuntimeException("名称、价格、库存数量不能为空");
        }
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        item.setStatus(2);//默认为下架状态
        itemMapper.insert(item);
    }

    //4.根据id修改商品状态
    @Override
    @Transactional
    public void updateStatus(Long id, Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        item.setUpdateTime(new Date());
        itemMapper.updateById(item);

        //向MQ发送消息
        String routingKey = status == 1 ? "add" : "delete";
        rabbitTemplate.convertAndSend("item.direct", routingKey, id);
    }

    //5.修改商品
    @Override
    public void updated(Item item) {
        item.setUpdateTime(new Date());
        itemMapper.updateById(item);
    }

    //6.删除商品
    @Override
    public void delete(Long id) {
        itemMapper.deleteById(id);
    }

    //扣减库存
    @Override
    @Transactional
    public void deductStock(Long itemId, Integer num) {
        //查询该商品当前库存
        Item item = itemMapper.selectById(itemId);
        //更新库存
        Integer stock = item.getStock() - num;
        item.setStock(stock);
        //更新销量
        Integer sold = item.getSold() + num;
        item.setSold(sold);
        //库存耗尽需要下架
        if (stock.equals(0)) {
            item.setStatus(2);
            //向MQ发送消息
            String routingKey = "delete";
            rabbitTemplate.convertAndSend("item.direct", routingKey, itemId);
        }
        item.setUpdateTime(new Date());
        itemMapper.updateById(item);
    }
}
